package com.yangaobo.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yangaobo.dto.CourseInsertDTO;
import com.yangaobo.dto.CoursePageDTO;
import com.yangaobo.dto.CourseUpdateDTO;
import com.yangaobo.entity.Course;
import com.yangaobo.excel.CourseExcel;
import com.yangaobo.exception.ServerErrorException;
import com.yangaobo.mapper.CourseMapper;
import com.yangaobo.service.CourseService;
import com.yangaobo.vo.CourseVO;
import jakarta.annotation.Resource;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 杨奥博
 */
@Service
@CacheConfig(cacheNames = "Course")
public class CourseServiceImpl implements CourseService {
    @Resource
    private CourseMapper courseMapper;

    @CacheEvict(allEntries = true)
    @Override
    public int insert(CourseInsertDTO dto) {
        Course course = BeanUtil.copyProperties(dto, Course.class);
        if(StrUtil.isBlank(dto.getInfo())) {
            course.setInfo("暂无描述");
        }
        course.setDeleted(0);
        course.setVersion(0L);
        course.setUpdated(LocalDateTime.now());
        course.setCreated(LocalDateTime.now());
        int result = courseMapper.insert(course);
        if(result <= 0) {
            throw new ServerErrorException("DB添加失败");
        }
        return result;
    }

    @Cacheable(key = "#p0", condition = "#p0 != null", unless = "#result == null")
    @Override
    public Course select(Long id) {
        Course result = courseMapper.select(id);
        if(ObjectUtil.isNull(result)) {
            throw new ServerErrorException("DB查询失败");
        }
        return result;
    }

    @Cacheable(key = "#root.methodName", unless = "#result == null")
    @Override
    public List<CourseVO> list() {
        return courseMapper.list(new CoursePageDTO())
                .stream()
                .map(course -> BeanUtil.copyProperties(course, CourseVO.class))
                .collect(Collectors.toList());
    }

    @Cacheable(key = "#root.methodName + ':' + #p0.toString()",
            condition = "#p0 != null",
            unless = "#result == null")
    @Override
    public PageInfo<Course> page(CoursePageDTO dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        return new PageInfo<>(courseMapper.list(dto));
    }

    @CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int update(CourseUpdateDTO dto) {
        Course course = courseMapper.select(dto.getId());
        if (ObjectUtil.isNull(course)) {
            throw new ServerErrorException("DB查询失败");
        }
        BeanUtil.copyProperties(dto, course,
                CopyOptions.create().setIgnoreNullValue(true));
        course.setUpdated(LocalDateTime.now());
        int result = courseMapper.update(course);
        if(result <= 0) {
            throw new ServerErrorException("DB更新失败");
        }
        return result;
    }

    @CacheEvict(allEntries = true)
    @Override
    public int delete(Long id) {
        int result = courseMapper.delete(id);
        if(result <= 0) {
            throw new ServerErrorException("DB删除失败");
        }
        return result;
    }

    @CacheEvict(allEntries = true)
    @Override
    public int deleteBatch(List<Long> ids) {
        int result = courseMapper.deleteBatch(ids);
        if(result <= 0) {
            throw new ServerErrorException("DB删除失败");
        }
        return result;
    }

    @Override
    public List<CourseExcel> getExcelData() {
        return courseMapper.list(new CoursePageDTO())
                .stream()
                .map(course -> {
                    CourseExcel courseExcel = BeanUtil.copyProperties(course, CourseExcel.class);
                    return courseExcel;
                })
                .collect(Collectors.toList());
    }

}
